package com.smasher.backup.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment

abstract class BaseDialogFragment : DialogFragment() {

    private var mFirstLoad = true
    private val debug = false

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (debug) {
            Log.d(TAG, "onAttach: ")
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (debug) {
            Log.d(TAG, "onCreate: ")
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (debug) {
            Log.d(TAG, "onCreateView: ")
        }
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (debug) {
            Log.d(TAG, "onViewCreated: ")
        }
        initView()
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        if (debug) {
            Log.d(TAG, "onCreateDialog: ")
        }
        return super.onCreateDialog(savedInstanceState)
    }

    override fun onStart() {
        super.onStart()
        if (debug) {
            Log.d(TAG, "onStart: ")
        }
    }

    override fun dismiss() {
        super.dismiss()
        if (debug) {
            Log.d(TAG, "dismiss: ")
        }
    }

    override fun dismissAllowingStateLoss() {
        super.dismissAllowingStateLoss()
        if (debug) {
            Log.d(TAG, "dismissAllowingStateLoss: ")
        }
    }

    override fun dismissNow() {
        super.dismissNow()
        if (debug) {
            Log.d(TAG, "dismissNow: ")
        }
    }

    override fun onResume() {
        super.onResume()
        if (debug) {
            Log.d(TAG, "onResume: ")
        }
        if (mFirstLoad) {
            this.initData()
            mFirstLoad = false
        }
    }

    override fun onPause() {
        super.onPause()
        if (debug) {
            Log.d(TAG, "onPause: ")
        }
    }

    override fun onStop() {
        super.onStop()
        if (debug) {
            Log.d(TAG, "onStop: ")
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        if (debug) {
            Log.d(TAG, "onDestroyView: ")
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (debug) {
            Log.d(TAG, "onDestroy: ")
        }
    }

    override fun onDetach() {
        super.onDetach()
        if (debug) {
            Log.d(TAG, "onDetach: ")
        }
    }

    fun isShowing(): Boolean {
        return dialog?.isShowing == true
    }

    protected abstract fun initData()

    protected abstract fun initView()

    companion object {
        const val TAG = "PROGRESS"
    }

}